<?

require_once "mods/function.php";

$_t1=microtime()+time();

//$home="<a href=index.php>home</a>";


if (!isset($_GET[toprint])) include("mods/header.php");
$mod=$_GET[mod];

switch ($mod){
	case 'editmod': 
		if ($_SESSION["adminmode"]){
			$modname=$_GET['modname'];
			if (file_exists("mods/$modname.php")){
				$body=file_get_contents("mods/$modname.php");
			}
			echo "<form action=index.php?mod=savemod method=post>
<h2>edit module:: \"$modname\"</h2>
<script>
function getpos(e){
	var pos=document.getElementById('pos');
	var body=document.getElementById('body');
	var s=body.value.substr(0,body.selectionStart);
	var i=1;
	var startpos=s.indexOf(\"\\n\",0);;var position=startpos;

	if (e.keyCode==9){
		var sel=body.selectionStart;
		var s1=body.value.substr(sel,(body.value.length-body.selectionEnd));
		body.value=s+\"\\t\"+s1;
		body.selectionStart=sel+1;
		body.selectionEnd=sel+1;
		body.focus();
		return false;
	}
	while (startpos>=0 && i<2000){
		i++;
		position=startpos;
		startpos=s.indexOf(\"\\n\",position+1);
	}	
	pos.innerHTML='row='+i+':col='+(body.selectionStart-position);
}
function searchstr(){
	var body=document.getElementById('body');
	var str=document.getElementById('search');
	var s=body.value.indexOf(str.value,body.selectionStart+1);
	body.selectionStart=s;	
	body.selectionEnd=s+str.value.length;	
	body.focus();
	return true;
}
 </script>
<table cellspacing=0 border=1 width=100%>
<tr><td>Module name:<input type=text name=name size=40 value='$modname'> Search:<input id=search ><input type=button onClick=\"searchstr();\" value='search'>
<input type=submit value=update> <span id=pos></span></td></tr>
<tr><td><textarea name=body cols=80 rows=40 wrap=off style='width:100%;'
onKeyPress=\"return getpos(event);\" id=body>".htmlspecialchars($body,ENT_QUOTES)."</textarea></td></tr>
</table>
</form>";
		} else {
			echo "<h2>no permisions</h2>$home";
		}
		break;
	case 'savemod':
		if ($_SESSION["adminmode"]) {
			$modname=$_POST['name'];
			$body=stripslashes($_POST['body']);
			$fp=fopen("mods/$modname.php","w");
			fputs($fp,$body);
			fclose($fp);
			echo "<h2>module \"$modname\" saved</h2>"; 
		} else {
			echo "<h2>no permisions</h2>";
		}
		break;
	case 'dropmod':
		if ($_SESSION["adminmode"]) {
			$modname=$_GET['modname'];
			if (unlink("mods/$modname.php")) echo "<h2>module \"$modname\" deleted</h2>"; 
			else echo "<h2>module \"$modname\" noexists</h2>$home";
		} else {
			echo "<h2>no permisions</h2>";
		}
		break;
	default: 
		if (file_exists("mods/$_GET[mod].php")) {
			cutTags($_POST);
			cutTags($_GET);
			include("mods/$_GET[mod].php");
		} else {
			echo "<h2>module does not exists</h2>";
		}
		
}//switch

if (!isset($_GET[toprint])) include("mods/footer.php");
sess_close();
?>